<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of frontApi
 *
 * @author Administrator
 */
class frontAction extends BaseAction{
    
    private $re = array('code' => 'erorr','msg' =>'缺少参数');

    /**
     * 获取连接句柄
     * @return WeiboClient 
     */
    private function getSinaAppConn(){
        include_once(ROOT. '/cgi/sina/config.php' );
        include_once(ROOT. '/cgi/sina/weibooauth.php' );

        return  new WeiboClient( WB_AKEY , WB_SKEY ,  token_key, token_secret);
    }
    /**
     * 生成腾讯短连接
     */
    function actionTencentUrl() {
        set_time_limit(60);
        $uhash    = $this->postVar('uhash');
        $re = array('code' => 'error','msg'=>'参数为空');
        if(!empty($uhash)) {
            $rs = DAO_Finder::daoOne(DB_URL, array('uhash' => $uhash));
            if($rs) {
                $url    = $rs->getUrl();
                $re   = trim(file_get_contents('http://u22.cc/servlet/tencent_url/url/'.  base64_encode($url)));
                if(strlen($re) == 6) {
                    $rs->setTencentUrl($re);
                    $rs->save();
                    $re = array('code' => 'ok','msg' => $re);
                }else{
                    $re['msg'] = '错误原因：'.$re.'\n请联系我们.';
                }
            }
        }else{
            //$re   = '';
        }
        echo json_encode($re);
    }

    /**
     * 生成新浪短连接
     */
    function actionSinaUrl() {
        set_time_limit(60);
        $uhash    = $this->postVar('uhash');
        $re = array('code' => 'error','msg'=>'参数为空');
        if(!empty($uhash)) {
            $rs = DAO_Finder::daoOne(DB_URL, array('uhash' => $uhash));
            if($rs) {
                $url    = $rs->getUrl();
                $re   = trim(file_get_contents('http://u22.cc/servlet/sina_url/url/'.  base64_encode($url)));
                if(strlen($re) == 6) {
                    $rs->setSinaUrl($re);
                    $rs->save();
                    $re = array('code' => 'ok','msg' => $re);
                }else{
                    $re['msg'] = '错误原因：'.$re.'\n请联系我们.';
                }
            }
        }else{
            //$re   = '';
        }
        echo json_encode($re);
    }
    
    /**
     * 使用新浪几口生成短网址
     */
    function actionSina() {
        include_once(ROOT. '/cgi/sina/config.php' );
        include_once(ROOT. '/cgi/sina/weibooauth.php' );
        $re = array('code' => 'error');
        $url    = $this->params['url'];
        if($url) {
            $c = new WeiboClient( WB_AKEY , WB_SKEY ,  token_key, token_secret);
            $ms  = $c->home_timeline(); // done
            $me = $c->short_url($url);
            if(array_key_exists(0, $me)){
                $re = $me[0];
                $re['url']  = str_replace('http://t.cn/', '', $re['url_short']);
            }
        }
        echo json_encode($re);
    }
    
    /**
     * 取得一个短链接在微博上的微博分享数（包含原创和转发的微博） 
     */
    function ActionSinaShortUrlShareCounts(){
        $c  = $this->getSinaAppConn();
        $url    = $this->params['url'];
        if(!empty($url)) {
            $url= 'http://t.cn/'.trim($url);
            //echo $url;
            $me  = $c->short_url_share_counts($url);
            if(array_key_exists(0, $me)){
                $this->re   = $me[0];
                $this->re['code'] = 'ok';
            }
        }
        echo json_encode($this->re);
    }
    
    /**
     * 取得一个短链接在微博上的微博评论数 
     */
    function ActionSinaShortUrlCommentCounts() {
        $c  = $this->getSinaAppConn();
        $url    = $this->params['url'];
        if(!empty($url)) {
            $url= 'http://t.cn/'.trim($url);
            //echo $url;
            $me  = $c->short_url_comment_counts($url);
            if(array_key_exists(0, $me)){
                $this->re   = $me[0];
                $this->re['code'] = 'ok';
            }
        }
        echo json_encode($this->re);  
    }
    
    /**
     * 取得一个短链接在微博上的微博评论
     */
    function ActionSinaShortUrlSharesSatuses() {
        $c  = $this->getSinaAppConn();
        $url    = $this->params['url'];
        if(!empty($url)) {
            $url= 'http://t.cn/'.trim($url);
            //echo $url;
            $me  = $c->short_url_share_statuses($url);
            if(array_key_exists('share_statuses', $me)){
                $this->re   = $me['share_statuses'];
                $this->re['code'] = 'ok';
            }
        }
        echo json_encode($this->re);  
    }
}
?>
